package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _21反转字符串中的单词
 * @description
 * @date 2023/07/14 16:11
 * <p>
 * ******************************************************
 */
public class _21反转字符串中的单词 {

    public String reverseWords1(String s) {
        int start = s.length() - 1;
        int end = s.length();
        String temp;
        StringBuilder sb = new StringBuilder();
        while (start >= 0) {
            temp = s.substring(start, end).trim();
            if ((s.charAt(start) == ' ' && temp.length() > 0) || start == 0) {
                sb.append(temp).append(" ");
                end = start;
            }
            start--;
        }
        return sb.toString().trim();
    }

    public String reverseWords(String s) {
        String[] words = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (String word : words) {
            if (word == null || word.trim().isEmpty()) {
                continue;
            }
            sb.append(new StringBuilder(word).reverse()).append(" ");
        }
        return sb.reverse().substring(1);
    }




    public static void main(String[] args) {
        _21反转字符串中的单词 s = new _21反转字符串中的单词();
        System.out.println(s.reverseWords("the sky is blue"));
    }
}
